package com.yungu.swift.socket.push.task;

import com.yungu.swift.socket.component.ChannelKeeper;
import com.yungu.swift.socket.component.PushContentMap;
import com.yungu.swift.socket.push.jiguang.JPushConfig;
import com.yungu.swift.socket.push.jiguang.JPushHelper;
import com.yungu.swift.socket.push.model.PushContent;
import com.yungu.swift.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * SimplePushTask
 *
 * @author cuixiuyin
 * @date 2018/12/27
 */
@Slf4j
public class SimplePushTask implements PushTask {

    /**
     * 发送目标用户
     */
    private List<String> targetUuids;
    /**
     * 推送报文
     */
    private PushContent pushContent;
    /**
     * 极光推送配置
     */
    private JPushConfig jPushConfig;

    //重试间隔时间（毫秒）
    private static final Integer SLEEP_MILLISECOND = 3000;

    public SimplePushTask(List<String> targetUuids, PushContent pushContent, JPushConfig jPushConfig) {
        this.targetUuids = targetUuids;
        this.pushContent = pushContent;
        this.jPushConfig = jPushConfig;
    }

    @Override
    public void nettyPush(String targetUuid) {
        ChannelKeeper.sendMessage(targetUuid, JsonUtils.toJSONString(pushContent));
    }

    @Override
    public void jPush() {
        //初始化极光推送对象
        JPushHelper jPushHelper = new JPushHelper(jPushConfig);
        String title = (String) pushContent.getData().get("title");
        String content = (String) pushContent.getData().get("content");
        Map<String, String> extras = (Map<String, String>) pushContent.getData().get("extras");
        //进行推送
        jPushHelper.pushMessage4AndroidWithAlias((String[]) targetUuids.toArray(), title, content, extras);
        jPushHelper.pushNotification4IosWithAlias((String[]) targetUuids.toArray(), content, 0, extras);
    }

    @Override
    public void run() {
        try {
            PushContentMap.getInstance().put(pushContent.getPushUuid(), pushContent);
            log.info("-----------send pushUuid:{}-----------", pushContent.getPushUuid());
            while (true) {
                if (canPush()) {
                    int retryCount = pushContent.getRetry();
                    if (retryCount > 0) {
                        log.info(pushContent.getPushUuid() + " retry " + retryCount + " times");
                    }
                    log.info("---------Send PushUuid[retry=" + retryCount + "]:" + pushContent.getPushUuid() + "----");
                    log.info("Data >>:" + pushContent.getData());
                    for (String targetUuid : targetUuids) {
                        nettyPush(targetUuid);
                    }
                    //增加重试次数
                    pushContent.setRetry(retryCount + 1);
                } else {
                    break;
                }
                Thread.sleep(SLEEP_MILLISECOND);
            }
        } catch (Exception e) {
            log.error("--------------- send msg error -------------:", e);
            e.printStackTrace();
        }
    }

    /**
     * 判断是否可以推送
     *
     * @return
     */
    private boolean canPush() {
        return pushContent.getRetry() < 3 && PushContentMap.getInstance().containsKey(pushContent.getPushUuid());
    }

}
